﻿function CustomerGroup() {
    var customerGroupID;
    var customerGroupName;
    var checked;

    this.setCustomerGroup = function (pcustomerGroupID, pcustomerGroupName) {
        this.customerGroupID = pcustomerGroupID;
        this.customerGroupName = pcustomerGroupName;
        this.checked = false;
    }
}

function Customer() {
    var customerID;
    var customerName;
    var marker;
    var checked;
    var customerGroupName;
    var numPos;
    var position;
    var address;
    var kms;
    var kmsText;
    var duration;
    var durationText;

    this.setCustomer = function (pcustomerID, pcustomerName, platitude, plongitude) {
        this.customerID = pcustomerID;
        this.customerName = pcustomerName;

        this.marker = objMapControl.addMarker(images.locationCustomer, objMapControl.addPosition(platitude, plongitude), pcustomerName);
        this.marker.setVisible(false);

        this.position = objMapControl.addPosition(stringToFloatForPosition(platitude), stringToFloatForPosition(plongitude));
        this.address = this.position.lat() + "," + this.position.lng();
        this.checked = false;
        this.numPos = null;
        this.kms = null;
        this.kmsText = null;
        this.duration = null;
        this.durationText = null;
    }
}

function StopMarker() {
    var stopMarker;
    var infoWindow;
    var initialTimeStop;
    var finalTimeStop;

    this.setStopMarker = function (stopMarker, infoWindow, initialTimeStop) {
        this.stopMarker = stopMarker;
        this.infoWindow = infoWindow;
        this.initialTimeStop = initialTimeStop;
    }
}

function User() {

    var username;
    var name;
    var firstName;
    var lastName;

    this.setUser = function (username, name, firstName, lastName) {
        this.username = username;
        this.name = name;
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

function Imei() {

    var imei;
    var name;
    var image;
    var position;
    var address;

    this.setImei = function (imei, name, image, latitude, longitude) {
        this.imei = imei;
        this.name = name;
        this.position = objMapControl.addPosition(stringToFloatForPosition(latitude), stringToFloatForPosition(longitude));
        this.image = objMapControl.addImage(image);
        this.address = this.position.lat() + "," + this.position.lng();
    }
    
}

function TrackData() {

    var imei;
    var hour;
    var speed;
    var position;
    var image;
    var tipoVehiculo;
    var marker;
    var currentKm;

    this.setTrackData = function (pimei, pspeed, platitude, plongitude, pimageLocation, phour) {
        this.imei = pimei;
        this.hour = phour;
        this.speed = pspeed;
        this.position = objMapControl.addPosition(stringToFloatForPosition(platitude), stringToFloatForPosition(plongitude));
        this.image = objMapControl.addImage(pimageLocation);
    };
}

function TrackDataLastLocationImei() {

    var imei;
    var imeiName;
    var imeiDescription;
    var imeiSimNumber;
    var imeiTypeID;
    var imeiTypeIDDescription;
    var gpsModelID;
    var gpsModelIDName;
    var image;
    var imageLocation;

    var lastMessageImei;

    var position;
    var marker;
    var speed = 0;
    var geoferenceExitAlarm = null;
    var geoferenceEnterAlarm = null;
    var sosAlarm = null;

    this.setInfo = function (pimei, pimeiName, pimeiDescription, pimeiSimNumber, pimeiTypeID, pimeiTypeIDDescription, platitude, plongitude, pimage, plastMessageImei) {

        this.imei = pimei;
        this.imeiName = pimeiName;
        this.imeiDescription = pimeiDescription.replace(/^\s*|\s*$/g, "");
        this.imeiSimNumber = pimeiSimNumber;
        this.imeiTypeID = pimeiTypeID;
        this.imeiTypeIDDescription = pimeiTypeIDDescription;
        var latitude = parseFloat(platitude.toString().replace(",", "."));
        var longitude = parseFloat(plongitude.toString().replace(",", "."));
        this.imageLocation = images.location + pimage;
        this.image = objMapControl.addImage(pimage);//new google.maps.MarkerImage(images.location + pimage);

        this.position = objMapControl.addPosition(latitude, longitude);//new google.maps.LatLng(latitude, longitude);
        this.marker = objMapControl.addMarker(this.image, null, this.imeiDescription);
        this.lastMessageImei = plastMessageImei;


    };

    this.updatePosition = function (platitude, plongitude, pspeed) {
        var latitude = parseFloat(platitude.toString().replace(",", "."));
        var longitude = parseFloat(plongitude.toString().replace(",", "."));

        this.position = objMapControl.addPosition(latitude, longitude);
        objMapControl.setPositionMarker(this.marker, this.position);
        this.speed = pspeed;
    }

    this.updateAlamsNotifications = function (pgeoferenceExitAlarm, pgeoferenceEnterAlarm, pSOSAlarm) {
        if (this.geoferenceExitAlarm != pgeoferenceExitAlarm && pgeoferenceExitAlarm == 'true') {

            addNotificationHistory('El vehículo ' + this.imeiDescription + ' salió del rango permitido.');
            showNotification('El vehículo salió del rango permitido.', this.imageLocation, this.imeiDescription);
        }

        this.geoferenceExitAlarm = pgeoferenceExitAlarm;

        if (this.geoferenceEnterAlarm != pgeoferenceEnterAlarm && pgeoferenceEnterAlarm == 'true') {
            addNotificationHistory('El vehículo ' + this.imeiDescription + ' ingreso al rango permitido.');
            showNotification('El vehículo ingreso al rango permitido.', this.imageLocation, this.imeiDescription);
        }

        this.geoferenceEnterAlarm = pgeoferenceEnterAlarm;

        if (this.sosAlarm != pSOSAlarm && pSOSAlarm == 'true') {
            addNotificationHistory('El vehículo ' + this.imeiDescription + ' ha activado la Alarma SOS.');
            showNotification('El vehículo ha activado la Alarma SOS.', this.imageLocation, this.imeiDescription);
        }

        this.sosAlarm = pSOSAlarm;
    }

    this.centerMarker = function () {
        objMapControl.setCenterMap(this.position);
    }
}

function XmlControl() {

    this.openXMLTrackDataLastLocationImei = function (xmlObject) {
        var xmlImei = jQuery.parseXML(xmlObject);
        var index;

        var imei;
        var latitude;
        var longitude;
        var geoferenceEnterAlarm;
        var geoferenceExitAlarm;
        var sosAlarm;
        var speed;

        jQuery(xmlImei).find('NewDataSet').each(function () {
            jQuery(this).find('Table').each(function () {

                imei = jQuery(this).find('Imei').text();
                latitude = jQuery(this).find('Latitude').text();
                longitude = jQuery(this).find('Longitude').text();
                geoferenceEnterAlarm = jQuery(this).find('GeoferenceEnterAlarm').text();
                geoferenceExitAlarm = jQuery(this).find('GeoferenceExitAlarm').text();
                sosAlarm = jQuery(this).find('SOSAlarm').text();
                speed = jQuery(this).find('Speed').text();
                index = existTrackDataLastLocationIntoTheList(imei);

                if (index != -1) {
                    globalList.trackDataLastLocation[index].updatePosition(latitude, longitude, speed);
                    globalList.trackDataLastLocation[index].updateAlamsNotifications(geoferenceExitAlarm, geoferenceEnterAlarm, sosAlarm);
                }
            });
        });
    }

    this.openXMLTrackDataLastLocationGroupsImei = function (xmlObject) {
        var xmlGroups = jQuery.parseXML(xmlObject);

        var tree = getMenuVehicleTree();
        var nodeGroup;
        var nodeGroupInserted;
        var childNodeVehicle;
        var lastGroupValue = "";
        var myTrackDataLastLocationImei;

        var groupIDvehicle;
        var groupName;

        var imei;
        var imeiName;
        var imeiDescription;
        var imeiSimNumber;
        var imeiTypeID;
        var ImeiTypeIDDescription;
        var gpsModelID;
        var gpsModelIDName;
        var image;
        var lastMessageImei;

        var latitude;
        var longitude;

        jQuery(xmlGroups).find('NewDataSet').each(function () {
            jQuery(this).find('Table').each(function () {

                groupIDvehicle = jQuery(this).find('GroupID').text();
                groupName = jQuery(this).find('GroupName').text();
                imei = jQuery(this).find('Imei').text();
                imeiName = jQuery(this).find('ImeiName').text();
                imeiDescription = jQuery(this).find('ImeiDescription').text();
                imeiTypeID = jQuery(this).find('ImeiTypeID').text();
                ImeiTypeIDDescription = jQuery(this).find('ImeiTypeIDDescription').text();
                imeiSimNumber = jQuery(this).find('ImeiSimNumber').text();
                gpsModelID = jQuery(this).find('GPSModelID').text();
                gpsModelIDName = jQuery(this).find('GPSModelIDName').text();
                latitude = jQuery(this).find('Latitude').text();
                longitude = jQuery(this).find('Longitude').text();
                image = jQuery(this).find('Image').text();
                lastMessageImei = jQuery(this).find('LastMessageImei').text();

                tree.trackChanges();

                nodeGroup = new Telerik.Web.UI.RadTreeNode();

                nodeGroup.set_value(groupIDvehicle);
                nodeGroup.set_text(groupName);
                nodeGroup.set_expanded(true);

                childNodeVehicle = new Telerik.Web.UI.RadTreeNode();

                childNodeVehicle.set_value(imei);
                childNodeVehicle.set_text(imeiName);
                childNodeVehicle.set_imageUrl(images.location + image);

                if (lastGroupValue != groupIDvehicle) {
                    nodeGroupInserted = tree.findNodeByValue(groupIDvehicle);
                }

                if (nodeGroupInserted == null) {
                    tree.get_nodes().add(nodeGroup);
                    nodeGroupInserted = tree.findNodeByValue(groupIDvehicle);
                    tree.get_nodes().add(childNodeVehicle);
                }

                nodeGroupInserted.get_nodes().add(childNodeVehicle);

                lastGroupValue = groupIDvehicle;

                tree.commitChanges();

                myTrackDataLastLocationImei = new TrackDataLastLocationImei();
                myTrackDataLastLocationImei.setInfo(imei, imeiName, imeiDescription, imeiSimNumber, imeiTypeID, ImeiTypeIDDescription, latitude, longitude, image, lastMessageImei);

                if (existTrackDataLastLocationIntoTheList(myTrackDataLastLocationImei.imei) == -1) {
                    globalList.trackDataLastLocation.push(myTrackDataLastLocationImei);
                }
            });
        });
    }

    this.openXMLGeoFenceDetailsByGeoFenceID = function (xmlObject) {
        var xmlGeoFence = jQuery.parseXML(xmlObject);

        var geoFenceID;
        var description;
        var imei;
        var numPosList = [];
        var latitudeList = [];
        var longitudeList = [];

        var myGeoFence = new GeoFence();
        //var myGeoFenceInfo = new GeoFenceInfo();

        jQuery(xmlGeoFence).find('NewDataSet').each(function () {
            jQuery(this).find('Table').each(function () {

                geoFenceID = jQuery(this).find('GeoFenceID').text();
                description = jQuery(this).find('Description').text();
                imei = jQuery(this).find('Imei').text();
                numPosList.push(jQuery(this).find('NumPos').text());
                latitudeList.push(jQuery(this).find('Latitude').text());
                longitudeList.push(jQuery(this).find('Longitude').text());
            });
        });

        myGeoFence.createGeoFence(geoFenceID, description, imei, numPosList, latitudeList, longitudeList);
        //myGeoFence.show = true;
        myGeoFence.checked = true;
        globalList.geoFence.push(myGeoFence);

        //myGeoFenceInfo.setGeoFenceInfo(geoFenceID, description, imei);
        //gGeoFenceInfoList.push(myGeoFenceInfo);
        objMapControl.clearGeoFenceMarkers(false);
        loadGeoFenceGridByImei(myGeoFence.imei);
        geoFence_ShowAllGeoFenceIDChecked();
        //meg_addAndSelectGeoFenceComboBox(myGeoFence);
    }

    this.openXMLAndUpdateGeoFenceDetailsByGeoFenceID = function (xmlObject) {
        var xmlGeoFenceDetails = jQuery.parseXML(xmlObject);

        var geoFenceID;
        var description;
        var imei;
        var numPosList = [];
        var latitudeList = [];
        var longitudeList = [];

        jQuery(xmlGeoFenceDetails).find('NewDataSet').each(function () {
            jQuery(this).find('Table').each(function () {

                geoFenceID = jQuery(this).find('GeoFenceID').text();
                description = jQuery(this).find('Description').text();
                imei = jQuery(this).find('Imei').text();

                numPosList.push(jQuery(this).find('NumPos').text());
                latitudeList.push(jQuery(this).find('Latitude').text());
                longitudeList.push(jQuery(this).find('Longitude').text());
            });
        });

        for (ii = 0; ii < globalList.geoFence.length; ii++) {
            if (geoFenceID == globalList.geoFence[ii].geoFenceID) {
                globalList.geoFence[ii].hideGeoFence();
                globalList.geoFence[ii].updateGeoFence(geoFenceID, description, imei, numPosList, latitudeList, longitudeList);

            }
        }

        /*for (ii = 0; ii < gGeoFenceInfoList.length; ii++) {
            if (geoFenceID == gGeoFenceInfoList[ii].geoFenceID) {
                gGeoFenceInfoList[ii].description = description;
                //meg_refreshGeoFenceInfoBomboBox();
            }
        }*/
    }

    this.openXMLGeoFenceDetails = function (xmlObject) {
        var xmlGeoFenceDetails = jQuery.parseXML(xmlObject);

        var geoFenceID;
        var description;
        var imei;
        var numPosList = [];
        var latitudeList = [];
        var longitudeList = [];

        var LastGeoFenceID = "";

        var myGeoFence;

        jQuery(xmlGeoFenceDetails).find('NewDataSet').each(function () {
            jQuery(this).find('Table1').each(function () {

                geoFenceID = jQuery(this).find('GeoFenceID').text();
                description = jQuery(this).find('Description').text();
                imei = jQuery(this).find('Imei').text();

                if (geoFenceID != LastGeoFenceID) {

                    if (LastGeoFenceID != "") {
                        globalList.geoFence.push(myGeoFence);
                    }

                    myGeoFence = new GeoFence();

                    numPosList = [];
                    latitudeList = [];
                    longitudeList = [];

                    numPosList.push(jQuery(this).find('NumPos').text());
                    latitudeList.push(jQuery(this).find('Latitude').text());
                    longitudeList.push(jQuery(this).find('Longitude').text());

                    myGeoFence.createGeoFence(geoFenceID, description, imei, numPosList, latitudeList, longitudeList);
                }
                else {
                    numPosList.push(jQuery(this).find('NumPos').text());
                    latitudeList.push(jQuery(this).find('Latitude').text());
                    longitudeList.push(jQuery(this).find('Longitude').text());

                    myGeoFence.numPosList = numPosList;
                    myGeoFence.latitudeList = latitudeList;
                    myGeoFence.longitudeList = longitudeList;
                }

                LastGeoFenceID = geoFenceID;
            });
        });

        if (LastGeoFenceID != "") {
            globalList.geoFence.push(myGeoFence);
        }

        //meg_addAndSelectGeoFenceComboBox(myGeoFence);
    }

    this.openCollectionStringTrackDataByImei = function (stringCollection) {

        hideHistoryRoute();
        totalList = globalList.trackDataByImei.length;
        for (k = 0; k < totalList; k++) {
            globalList.trackDataByImei.pop();
        }
        //mycar.setPosition(null);
        objMapControl.setPositionMarker(objMapControl.car, null);

        //var xmlImei = jQuery.parseXML(xmlObject);

        var kmAnterior;
        var kmActual;
        var kmtotal;

        var imei;
        var hour;
        var latitude;
        var longitude;
        var speed;
        var imageLocation;

        var myTrackData;
        var myLastTrackData = null;

        var info = [];

        for (i = 0; i < stringCollection.length; i++) {

            info = [];

            info = stringCollection[i].split('&');

            if (i == 0) {
                imei = info[0];
                imageLocation = info[1];
            }
            else {
                hour = info[0];
                speed = info[1];
                latitude = info[2];
                longitude = info[3];

                myTrackData = new TrackData();

                kmAnterior = 0;
                kmActual = 0;
                kmtotal = 0;

                myTrackData.setTrackData(imei, speed, latitude, longitude, imageLocation, hour);

                if (myLastTrackData != null) {
                    kmAnterior = myLastTrackData.currentKm;
                    kmActual = objMapControl.getDistanceKms(myLastTrackData.position, myTrackData.position);

                    kmtotal = parseFloat(kmAnterior) + parseFloat(kmActual);
                    myTrackData.currentKm = kmtotal;
                }
                else {
                    myTrackData.currentKm = 0;
                }

                globalList.trackDataByImei.push(myTrackData);
                myLastTrackData = myTrackData;
            }
        }
    }

    this.openXMLCustomersByUsernameAndCustomerGroupID = function (xmlObject) {
        var xmlCustomers = jQuery.parseXML(xmlObject);

        var customerID;
        var customerName;
        var latitude;
        var longitude;

        var myCustomer;

        jQuery(xmlCustomers).find('NewDataSet').each(function () {
            jQuery(this).find('Table').each(function () {

                customerID = jQuery(this).find('CustomerID').text();
                customerName = jQuery(this).find('CustomerName').text();
                latitude = jQuery(this).find('Latitude').text();
                longitude = jQuery(this).find('Longitude').text();
                myCustomer = new Customer();
                myCustomer.setCustomer(customerID, customerName, latitude, longitude);

                globalList.customers.push(myCustomer);
            });
        });
    }

    this.openXMLUsersByAdmin = function (xmlObject) {
        var xmlUsers = jQuery.parseXML(xmlObject);

        var username;
        var name;
        var firstName;
        var lastName;

        var myUser;

        jQuery(xmlUsers).find('NewDataSet').each(function () {
            jQuery(this).find('Table').each(function () {

                username = jQuery(this).find('Username').text();
                name = jQuery(this).find('Name').text();
                firstName = jQuery(this).find('FirstName').text();
                lastName = jQuery(this).find('LastName').text();

                myUser = new User();
                myUser.setUser(username, name, firstName, lastName);

                globalList.users.push(myUser);
            });
        });
    }

    this.openXMLImeiByUser = function (xmlObject) {
        var xmlImei = jQuery.parseXML(xmlObject);

        var imei;
        var name;
        var image;
        var latitude;
        var longitude;

        var myImei;

        jQuery(xmlImei).find('NewDataSet').each(function () {
            jQuery(this).find('Table').each(function () {

                imei = jQuery(this).find('Imei').text();
                name = jQuery(this).find('Name').text();
                image = jQuery(this).find('Image').text();
                latitude = jQuery(this).find('Latitude').text();
                longitude = jQuery(this).find('Longitude').text();

                myImei = new Imei();
                myImei.setImei(imei, name, image, latitude, longitude);

                globalList.imei.push(myImei);
            });
        });

        
    }

    this.openXMLAllCustomerByUser = function (xmlObject) {
        var xmlCustomer = jQuery.parseXML(xmlObject);

        var customerID;
        var customerName;
        var customerGroupName;
        var latitude;
        var longitude;

        var myCustomer;

        clearList(globalList.allCustomers);

        jQuery(xmlCustomer).find('NewDataSet').each(function () {
            jQuery(this).find('Table').each(function () {

                customerID = jQuery(this).find('CustomerID').text();
                customerName = jQuery(this).find('CustomerName').text();
                customerGroupName = jQuery(this).find('CustomerGroupName').text();
                latitude = jQuery(this).find('Latitude').text();
                longitude = jQuery(this).find('Longitude').text();

                myCustomer = new Customer();
                myCustomer.setCustomer(customerID, customerName, latitude, longitude)
                myCustomer.customerGroupName = customerGroupName;
                myCustomer.checked = false;
                //myCustomer.setPosition(latitude, longitude);

                globalList.allCustomers.push(myCustomer);
            });
        });
    }

    this.openXMLCustomersGroupsByUsername = function (xmlObject) {
        var xmlCustomersGroups = jQuery.parseXML(xmlObject);

        var customerGroupID;
        var customerGroupName;

        var myCustomerGroup;

        jQuery(xmlCustomersGroups).find('NewDataSet').each(function () {
            jQuery(this).find('Table').each(function () {

                customerGroupID = jQuery(this).find('CustomerGroupID').text();
                customerGroupName = jQuery(this).find('CustomerGroupName').text();
                myCustomerGroup = new CustomerGroup();
                myCustomerGroup.setCustomerGroup(customerGroupID, customerGroupName);

                globalList.customersGroups.push(myCustomerGroup);
            });
        });
    }
}

